

capture program drop fig_0200
program define fig_0200

global bin=10
global binname=$bin
global bin_new=$bin * 2 
global binname_new=$bin * 2 

global zoom_in_c300     =80        
global zoom_out_c300    =470
global range_c300      "incbin${binname}>=float(${zoom_in_c300}) & incbin${binname}<=float(${zoom_out_c300})"


//2000  
use "${processed_data}\trading_rights_asif.dta"  , clear
if $winsor ==1 {
 _pctile  ${myvar},  p(1  99)
drop if ${myvar}<=r(r1) | ${myvar}>=r(r2) 
}
keep  if  ${firm}==1 & ${condition}  &  year==2000  & ${range}
gen tinctobin= RC/${bin} //registered capital; unit 10K RMB
gen incbinaux=floor(tinctobin)
gen incbin${binname}=incbinaux*${bin}
drop  tinctobin   incbinaux

gen tinctobin= RC/${bin_new} 
gen incbinaux=floor(tinctobin)
gen incbin${binname_new}=incbinaux*${bin_new}
drop  tinctobin   incbinaux
keep if ${range_c300}
collapse  numbin${binname_new}_L2=year (mean)  ${myvar}_L2 =${myvar} ,  by(incbin${binname_new}) 
save  "${causal_temp_dir}\temp_causal_2000.dta", replace


//2002
use "${processed_data}\trading_rights_asif.dta"  , clear
if $winsor ==1 {
 _pctile  ${myvar},  p(1  99)
drop if ${myvar}<=r(r1) | ${myvar}>=r(r2) 
}
keep  if  ${firm}==1 & ${condition}  &  year==2002  & ${range}
gen tinctobin= RC/${bin} 
gen incbinaux=floor(tinctobin)
gen incbin${binname}=incbinaux*${bin}
drop  tinctobin   incbinaux

gen tinctobin= RC/${bin_new}  
gen incbinaux=floor(tinctobin)
gen incbin${binname_new}=incbinaux*${bin_new}
drop  tinctobin   incbinaux
keep if ${range_c300}
collapse  (count) numbin${binname_new}=year    (mean)  ${myvar}=${myvar} ,  by(incbin${binname_new}) 
save  "${causal_temp_dir}\temp_causal_2002.dta", replace




use "${causal_temp_dir}\temp_causal_2000.dta", clear
merge 1:1 incbin${binname_new} using   "${causal_temp_dir}\temp_causal_2002.dta"
drop _merge


#delimit;
twoway
(connected ${myvar}_L2  incbin${binname_new}[fweight=numbin${binname_new}_L2] if incbin${binname_new}<$cutoff, sort clcolor(grey)   mcolor(grey)   msymbol(o) msize(small) lwidth(medthick) lpattern(dash) )
(connected ${myvar}  incbin${binname_new}[fweight=numbin${binname_new}] if incbin${binname_new}<$cutoff, sort clcolor(green)  mcolor(green)  msymbol(o) msize(small) lwidth(medthick) lpattern(solid))
(connected ${myvar}_L2  incbin${binname_new}[fweight=numbin${binname_new}_L2] if incbin${binname_new}>=$cutoff, sort clcolor(grey)   mcolor(grey)   msymbol(o) msize(small) lwidth(medthick) lpattern(dash) )
(connected ${myvar}  incbin${binname_new}[fweight=numbin${binname_new}] if incbin${binname_new}>=$cutoff, sort clcolor(green)  mcolor(green)  msymbol(o) msize(small) lwidth(medthick) lpattern(solid))
,
legend(  order(1 2)  label(1 "2000 Observed")   label(2 "2002 Observed")    size(small)  )
xline(${cutoff}, lcolor(black) lwidth(med))	xline(${graph_low}  ${graph_high}, lcolor(black) lwidth(medthin) lpattern(dash) )	  
ytitle(${myvar_name},size(small)  margin(medium))  
xtitle(Registered Capital(unit:10,000 RMB) in ${year_name}, size(small) margin(medium) ) xtitle(, alignment(top)) 						
xlabel(${zoom_in_c300} (20) ${zoom_out_c300}, labsize(small))ylabel(#5) 
graphregion(fcolor(white)  ifcolor(white) color(white) icolor(white))
title("${myvar_name}", justification(center) color(black) size(medsmall)) 
subtitle("for ${firm_name} firms in ${location} Over Time"  "${cutoff_note_rv}",  justification(center) color(black) size(medsmall))
note("Note: bin=${bin_new}; data: ASIF.""Cutoff=50 during Sep 2003~Jul 2004; =0 afterwards",size(small) )
;						
graph export "${causal_graph_dir}/rawfig_causal_02_00_${cutoff}_${firm_name}_${name}_${year_name}_${myvar}_.pdf",replace;
#delimit cr 


qui rm "${causal_temp_dir}\temp_causal_2000.dta"
qui rm "${causal_temp_dir}\temp_causal_2002.dta"
end 






capture program drop fig_020400
program define fig_020400

global bin=10
global binname=$bin
global bin_new=$bin * 2 
global binname_new=$bin * 2 

global zoom_in_c300     =80      
global zoom_out_c300    =470
global range_c300      "incbin${binname}>=float(${zoom_in_c300}) & incbin${binname}<=float(${zoom_out_c300})"


//2000  
use "${processed_data}\trading_rights_asif.dta"  , clear
if $winsor ==1 {
 _pctile  ${myvar},  p(1  99)
drop if ${myvar}<=r(r1) | ${myvar}>=r(r2) 
}
keep  if  ${firm}==1 & ${condition}  &  year==2000  & ${range}
gen tinctobin= RC/${bin} 
gen incbinaux=floor(tinctobin)
gen incbin${binname}=incbinaux*${bin}
drop  tinctobin   incbinaux

gen tinctobin= RC/${bin_new} 
gen incbinaux=floor(tinctobin)
gen incbin${binname_new}=incbinaux*${bin_new}
drop  tinctobin   incbinaux
keep if ${range_c300}
collapse  numbin${binname_new}_L2=year (mean)  ${myvar}_L2 =${myvar} ,  by(incbin${binname_new}) 
save  "${causal_temp_dir}\temp_causal_2000.dta", replace


//2002
use "${processed_data}\trading_rights_asif.dta"  , clear
if $winsor ==1 {
 _pctile  ${myvar},  p(1  99)
drop if ${myvar}<=r(r1) | ${myvar}>=r(r2) 
}
keep  if  ${firm}==1 & ${condition}  &  year==2002  & ${range}
gen tinctobin= RC/${bin} 
gen incbinaux=floor(tinctobin)
gen incbin${binname}=incbinaux*${bin}
drop  tinctobin   incbinaux

gen tinctobin= RC/${bin_new}  
gen incbinaux=floor(tinctobin)
gen incbin${binname_new}=incbinaux*${bin_new}
drop  tinctobin   incbinaux
keep if ${range_c300}
collapse  (count) numbin${binname_new}=year    (mean)  ${myvar}=${myvar} ,  by(incbin${binname_new}) 
save  "${causal_temp_dir}\temp_causal_2002.dta", replace



//2002 in 2004
use "${processed_data}\trading_rights_asif.dta"  , clear
if ${winsor} ==1 {
 _pctile  ${myvar}_F2,  p(1  99)
drop if ${myvar}_F2<=r(r1) | ${myvar}_F2>=r(r2) 
}
keep    if  ${firm}==1 & ${condition}  &  year==2002  & ${range}
gen tinctobin= RC/${bin} 
gen incbinaux=floor(tinctobin)
gen incbin${binname}=incbinaux*${bin}
drop  tinctobin   incbinaux

gen tinctobin= RC/${bin_new}  
gen incbinaux=floor(tinctobin)
gen incbin${binname_new}=incbinaux*${bin_new}
drop  tinctobin   incbinaux
keep if ${range_c300}
collapse  (count) numbin${binname_new}_F2=year    (mean)  ${myvar}_F2=${myvar}_F2 ,  by(incbin${binname_new}) 
save  "${causal_temp_dir}\temp_causal_2004.dta", replace



use "${causal_temp_dir}\temp_causal_2000.dta", clear
merge 1:1 incbin${binname_new} using   "${causal_temp_dir}\temp_causal_2002.dta"
drop _merge
merge 1:1 incbin${binname_new} using   "${causal_temp_dir}\temp_causal_2004.dta"
drop _merge 


#delimit;
twoway
(connected ${myvar}_L2  incbin${binname_new}[fweight=numbin${binname_new}_L2] if incbin${binname_new}<$cutoff , sort clcolor(grey)   mcolor(grey)   msymbol(o) msize(small) lwidth(medthick) lpattern(dash) )
(connected ${myvar}     incbin${binname_new}[fweight=numbin${binname_new}] if incbin${binname_new}<$cutoff , sort clcolor(green)  mcolor(green)  msymbol(d) msize(small) lwidth(medthick) lpattern(solid))
(connected ${myvar}_F2  incbin${binname_new}[fweight=numbin${binname_new}_F2] if incbin${binname_new}<$cutoff , sort clcolor(orange) mcolor(orange) msymbol(s) msize(small) lwidth(medthick) lpattern(solid))
(connected ${myvar}_L2  incbin${binname_new}[fweight=numbin${binname_new}_L2] if incbin${binname_new}>=$cutoff , sort clcolor(grey)   mcolor(grey)   msymbol(o) msize(small) lwidth(medthick) lpattern(dash) )
(connected ${myvar}     incbin${binname_new}[fweight=numbin${binname_new}]  if incbin${binname_new}>=$cutoff , sort clcolor(green)  mcolor(green)  msymbol(d) msize(small) lwidth(medthick) lpattern(solid))
(connected ${myvar}_F2  incbin${binname_new}[fweight=numbin${binname_new}_F2]  if incbin${binname_new}>=$cutoff , sort clcolor(orange) mcolor(orange) msymbol(s) msize(small) lwidth(medthick) lpattern(solid))
,
legend(order(1 2 3)  cols(3)  label(1 "2000 obsered")   label(2 "2002 observed")   label(3 "2004 observed")   size(small) )
xline(${cutoff}, lcolor(black) lwidth(med))	xline(${graph_low}  ${graph_high}, lcolor(black) lwidth(medthin) lpattern(dash) )	 
ytitle(${myvar_name},size(small)  margin(medium))  
xtitle(Registered Capital(unit:10,000 RMB) in ${year_name}, size(small) margin(medium) ) xtitle(, alignment(top)) 						
xlabel(${zoom_in_c300} (20) ${zoom_out_c300}, labsize(small))			
ylabel(#5) 
graphregion(fcolor(white)  ifcolor(white) color(white) icolor(white))
title("${myvar_name}", justification(center) color(black) size(medsmall)) 
subtitle("for ${firm_name} firms in ${location} Over Time"  "${cutoff_note_rv}",  justification(center) color(black) size(medsmall))
note("Note: bin=${bin_new}; data: ASIF.""Cutoff=50 during Sep 2003~Jul 2004; =0 afterwards",size(small) )
;						
graph export "${causal_graph_dir}/rawfig_causal_dynamic_${cutoff}_${firm_name}_${name}_${year_name}_${myvar}.pdf",replace;
#delimit cr 



qui rm "${causal_temp_dir}\temp_causal_2000.dta"
qui rm "${causal_temp_dir}\temp_causal_2002.dta"
qui rm "${causal_temp_dir}\temp_causal_2004.dta"
end 







/******2002: fitted*******/
capture program drop fig_1year
program define fig_1year
global bin=10
global binname=$bin
global bin_new=$bin *2
global data_in=20
global data_out=1180 
global range               "RC>=float(${data_in}) & RC<=float(${data_out})"
global zoom_in_c300=80        
global zoom_out_c300=470
global range_c300   "incbin${binname}>=float(${zoom_in_c300}) & incbin${binname}<=float(${zoom_out_c300})"

use "${processed_data}\trading_rights_asif.dta"  ,clear
if $winsor ==1 {
 _pctile  ${myvar},  p(1  99)
drop if ${myvar}<=r(r1) | ${myvar}>=r(r2) 
}
keep       if  ${firm}==1 & ${condition}  &  ${year_condition}  & ${range} 
gen tinctobin= RC/${bin} 
gen incbinaux=floor(tinctobin)
gen incbin${binname}=incbinaux*${bin}
drop  tinctobin   incbinaux

gen tinctobin= RC/${bin_new}  
gen incbinaux=floor(tinctobin)
gen incbin${binname_new}=incbinaux*${bin_new}
drop  tinctobin   incbinaux
keep if ${range_c300}

rename ${myvar}  y
gen RC_2=RC*RC
gen RC_3=RC_2*RC
gen D=0
replace D=1 if incbin${binname}>=float(${cutoff})
gen round50=0
replace round50=1  if mod(incbin${binname},50)==0
gen round100=0
replace round100=1  if  mod(incbin${binname},100)==0  
gen round50X  =round50*RC
gen round50X2 =round50*RC_2
gen round100X =round100*RC
gen round100X2=round100*RC_2
global rounding_q  "round50 round100 round50X  round100X  round50X2 round100X2"

gen y_nofe_ct=.
gen y_noferd_ct=.
qui tab cic_adj2, gen (sector)
global num_sector=r(r)	
reg y  D   RC   RC_2   $rounding_q  sector*    if (incbin${binname}<float(${graph_low}) ) | (incbin${binname}>float(${graph_high}) ), cluster(incbin${binname})
replace  y_noferd_ct=  _b[D]*D + _b[RC]*RC + _b[RC_2]*RC_2
if $num_sector == 23{
gen y_noferd =y  - _b[_cons]  - _b[sector1]*sector1 - _b[sector2]*sector2 - _b[sector3]*sector3 - _b[sector4]*sector4 - _b[sector5]*sector5 - _b[sector6]*sector6 - _b[sector7]*sector7 - _b[sector8]*sector8 - _b[sector9]*sector9 - _b[sector10]*sector10 - _b[sector11]*sector11 - _b[sector12]*sector12  - _b[sector13]*sector13 - _b[sector14]*sector14 - _b[sector15]*sector15 - _b[sector16]*sector16 - _b[sector17]*sector17 - _b[sector18]*sector18 - _b[sector19]*sector19 - _b[sector20]*sector20 - _b[sector21]*sector21 - _b[sector22]*sector22 - _b[sector23]*sector23 - (_b[round50]*round50 + _b[round100]*round100 + _b[round50X]*round50X + _b[round100X]*round100X + _b[round50X2]*round50X2 + _b[round100X2]*round100X2) 
 }

collapse (count) numbin${binname_new}=year (mean) y  y_noferd  D  y_noferd_ct, by(incbin${binname_new}) 
  

 
 #delimit;
qui twoway 
(scatter y_noferd   incbin${binname_new}[fweight=numbin${binname_new}], mcolor(green  green) msize(small small) ) 
(line  y_noferd_ct  incbin${binname_new} if incbin${binname_new}<float(${cutoff}),  sort clcolor(black)  mcolor(black) msymbol(o) lwidth(thick) lpattern(dash))
(line  y_noferd_ct  incbin${binname_new} if incbin${binname_new}>=float(${cutoff}) , sort clcolor(black)  mcolor(black) msymbol(o) lwidth(thick) lpattern(dash))
,
legend(order(1 2) cols(2) label(1 "Observed (net of FE & rounding)")   label(2 "Counterfactual (net of FE & rounding)")   size(vsmall) )
xline(${cutoff}, lcolor(black) lwidth(medium))	
xline(${graph_low}  ${graph_high}, lcolor(black) lwidth(medthin) lpattern(dash) )	 
ylabel(-0.03 (0.01) 0.04, labsize(small)) 
ytitle(${myvar_name}, size(small)  margin(medium))  
xtitle(Registered Capital (unit:10,000 RMB) in ${year_name}, size(small) margin(medium) ) xtitle(, alignment(top)) 						
xlabel(${zoom_in_c300} (20) ${zoom_out_c300}, labsize(small))		
graphregion(fcolor(white)  ifcolor(white) color(white) icolor(white))
title("${myvar_name}", justification(center) color(black) size(medsmall)) 
subtitle( "for ${firm_name} firms in ${location} ${year_note}"  "${cutoff_note}",  justification(center) color(black) size(medidum))
note("Data: ASIF.",size(small) )
;						
graph export   "${causal_graph_dir}/fitfig_causal_ynoferd_${cutoff}_${firm_name}_${name}_${year_name}_${myvar}.pdf"
,replace;
#delimit cr 

 
end 



